home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GXCPATH.C < prev    next >
C/C++ Source or Header  |  1992-03-24  |  22KB  |  700 lines

  1. /* Copyright (C) 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gxcpath.c */
  21. /* Implementation of clipping paths */
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gxdevice.h"
  25. #include "gxfixed.h"
  26. #include "gzcolor.h"
  27. #include "gzpath.h"
  28. #include "gxcpath.h"
  29.  
  30. #define min_int (-1 << (sizeof(int) * 8 - 1))
  31. #define max_int (~min_int)
  32.  
  33. const uint gs_clip_path_sizeof = sizeof(gx_clip_path);
  34.  
  35. /* Imported procedures */
  36. gx_device *gs_currentdevice(P1(gs_state *));
  37. void gx_set_device_only(P2(gs_state *, gx_device *));
  38.  
  39. /* Forward references */
  40. private void clip_prepare(P1(gx_clip_list *));
  41.  
  42. private const gx_clip_list clip_list_empty =
  43. {  { 0, 0, min_int, min_int, min_int, min_int },
  44.    { 0, 0, min_int, max_int, 0, 0 },
  45.    { 0, 0, max_int, max_int, max_int, max_int },
  46.    0
  47. };
  48.  
  49. /* Debugging */
  50.  
  51. #ifdef DEBUG
  52. #  define clip_rect_print(str, ar)\
  53.     if ( gs_debug['q'] )\
  54.     dprintf6("[q]%s %lx: (%d,%d),(%d,%d)\n", str, (ulong)ar,\
  55.          (ar)->xmin, (ar)->ymin, (ar)->xmax, (ar)->ymax)
  56. #else
  57. #  define clip_rect_print(s, ar) 0
  58. #endif
  59.  
  60. #ifdef DEBUG
  61. /* Validate a clipping path that has gone through clip_prepare. */
  62. private void
  63. clip_list_validate(gx_clip_list *clp)
  64. {    gx_clip_rect *prev = &clp->first;
  65.     gx_clip_rect *ptr = prev;
  66.     int wrong = 0;
  67.     while ( ptr != 0 )
  68.       { if ( ptr->ymin > ptr->ymax || ptr->xmin > ptr->xmax ||
  69.         !(ptr->ymin >= prev->ymax ||
  70.           ptr->ymin == prev->ymin && ptr->ymax == prev->ymax &&
  71.           ptr->xmin >= prev->xmax)
  72.         )
  73.           { clip_rect_print("WRONG:", ptr);
  74.         wrong = 1;
  75.           }
  76.         prev = ptr, ptr = ptr->next;
  77.       }
  78. }
  79. #endif
  80.  
  81. /* ------ Clipping path accessing ------ */
  82.  
  83. /* Return the path of a clipping path. */
  84. int
  85. gx_cpath_path(gx_clip_path *pcpath, gx_path *ppath)
  86. {    if ( !pcpath->segments_valid )
  87.        {    int code = gx_clip_list_add_to_path(&pcpath->list, &pcpath->path);
  88.         if ( code < 0 ) return code;
  89.         pcpath->segments_valid = 1;
  90.        }
  91.     *ppath = pcpath->path;
  92.     return 0;
  93. }
  94.  
  95. /* Return the quick-check rectangle for a clipping path. */
  96. int
  97. gx_cpath_box_for_check(gx_clip_path *pcpath, gs_fixed_rect *pbox)
  98. {    *pbox = pcpath->cbox;
  99.     return 0;
  100. }
  101.  
  102. /* Test if a clipping path includes a rectangle. */
  103. /* The rectangle need not be oriented correctly, i.e. x0 > x1 is OK. */
  104. int
  105. gx_cpath_includes_rectangle(register gx_clip_path *pcpath,
  106.   fixed x0, fixed y0, fixed x1, fixed y1)
  107. {    return
  108.         (x0 <= x1 ?
  109.             (pcpath->cbox.p.x <= x0 && x1 <= pcpath->cbox.q.x) :
  110.             (pcpath->cbox.p.x <= x1 && x0 <= pcpath->cbox.q.x)) &&
  111.         (y0 <= y1 ?
  112.             (pcpath->cbox.p.y <= y0 && y1 <= pcpath->cbox.q.y) :
  113.             (pcpath->cbox.p.y <= y1 && y0 <= pcpath->cbox.q.y));
  114. }
  115.  
  116. /* Release a clipping path. */
  117. void
  118. gx_cpath_release(gx_clip_path *pcpath)
  119. {    if ( !pcpath->shares_list )
  120.         gx_clip_list_free(&pcpath->list, &pcpath->path.memory_procs);
  121.     gx_path_release(&pcpath->path);
  122. }
  123.  
  124. /* Share a clipping path. */
  125. void
  126. gx_cpath_share(gx_clip_path *pcpath)
  127. {    gx_path_share(&pcpath->path);
  128.     pcpath->shares_list = 1;
  129. }
  130.  
  131. /* Create a rectangular clipping path. */
  132. /* The supplied rectangle may not be oriented correctly, */
  133. /* but it will be oriented correctly upon return. */
  134. int
  135. gx_cpath_from_rectangle(gx_clip_path *pcpath, gs_fixed_rect *pbox, gs_memory_procs *mp)
  136. {    gx_clip_list_from_rectangle(&pcpath->list, pbox);
  137.     pcpath->cbox = *pbox;
  138.     pcpath->segments_valid = 0;
  139.     pcpath->shares_list = 0;
  140.     gx_path_init(&pcpath->path, mp);
  141.     pcpath->path.bbox = *pbox;
  142.     return 0;
  143. }
  144.  
  145. /* Intersect a new clipping path with an old one. */
  146. /* Note that it may overwrite its path argument. */
  147. int
  148. gx_cpath_intersect(gs_state *pgs, gx_clip_path *pcpath, gx_path *ppath, int rule)
  149. {    gs_fixed_rect old_box, new_box;
  150.     if ( gx_cpath_is_rectangle(pcpath, &old_box) &&
  151.         gx_path_is_rectangle(ppath, &new_box)
  152.        )
  153.        {    int changed = 0;
  154.         /* Intersect the two rectangles if necessary. */
  155.         if ( old_box.p.x > new_box.p.x )
  156.             new_box.p.x = old_box.p.x, changed = 1;
  157.         if ( old_box.p.y > new_box.p.y )
  158.             new_box.p.y = old_box.p.y, changed = 1;
  159.         if ( old_box.q.x < new_box.q.x )
  160.             new_box.q.x = old_box.q.x, changed = 1;
  161.         if ( old_box.q.y < new_box.q.y )
  162.             new_box.q.y = old_box.q.y, changed = 1;
  163.         if ( changed )
  164.            {    /* Store the new rectangle back into the new path. */
  165.             register segment *pseg =
  166.                 (segment *)ppath->first_subpath;
  167. #define set_pt(pqx,pqy)\
  168.   pseg->pt.x = new_box.pqx.x, pseg->pt.y = new_box.pqy.y
  169.             set_pt(p, p); pseg = pseg->next;
  170.             set_pt(q, p); pseg = pseg->next;
  171.             set_pt(q, q); pseg = pseg->next;
  172.             set_pt(p, q); pseg = pseg->next;
  173.             set_pt(p, p);
  174. #undef set_pt
  175.            }
  176.         ppath->bbox = new_box;
  177.         gx_clip_list_from_rectangle(&pcpath->list, &new_box);
  178.         pcpath->cbox = new_box;
  179.         pcpath->path = *ppath;
  180.         pcpath->segments_valid = 1;
  181.        }
  182.     else
  183.        {    /* Not a rectangle.  Intersect the slow way. */
  184.         gx_device_accum adev;
  185.         gx_device_color devc;
  186.         gx_device *save_dev = gs_currentdevice(pgs);
  187.         int code;
  188.         adev = gs_accum_device;
  189.         adev.memory_procs = pcpath->path.memory_procs;
  190.         (*adev.procs->open_device)((gx_device *)&adev);
  191.         devc.color1 = devc.color2 = 0;    /* arbitrary, but not */
  192.                     /* transparent */
  193.         devc.halftone_level = 0;
  194.         gx_set_device_only(pgs, (gx_device *)&adev);
  195.         code = gx_fill_path(ppath, &devc, pgs, rule, (fixed)0);
  196.         gx_set_device_only(pgs, save_dev);
  197.         if ( code < 0 ) return code;
  198.         code = (*adev.procs->close_device)((gx_device *)&adev);
  199.         if ( code < 0 ) return code;
  200.         pcpath->list = adev.list;
  201.         /* The quick check must fail. */
  202.         pcpath->cbox.p.x = pcpath->cbox.p.y = 0;
  203.         pcpath->cbox.q.x = pcpath->cbox.q.y = 0;
  204.         gx_path_init(&pcpath->path, &pcpath->path.memory_procs);
  205.         pcpath->path.bbox.p.x = int2fixed(adev.bbox.p.x);
  206.         pcpath->path.bbox.p.y = int2fixed(adev.bbox.p.y);
  207.         pcpath->path.bbox.q.x = int2fixed(adev.bbox.q.x);
  208.         pcpath->path.bbox.q.y = int2fixed(adev.bbox.q.y);
  209.         pcpath->segments_valid = 0;
  210.         pcpath->shares_list = 0;
  211.        }
  212.     return 0;
  213. }
  214.  
  215. /* ------ Clipping list routines ------ */
  216.  
  217. /* Initialize a clip list. */
  218. void
  219. gx_clip_list_init(gx_clip_list *clp)
  220. {    *clp = clip_list_empty;
  221. }
  222.  
  223. /* Initialize a clip list to a rectangle. */
  224. /* The supplied rectangle may not be oriented correctly, */
  225. /* but it will be oriented correctly upon return. */
  226. void
  227. gx_clip_list_from_rectangle(gx_clip_list *clp, register gs_fixed_rect *rp)
  228. {    gx_clip_list_init(clp);
  229.     if ( rp->p.x > rp->q.x )
  230.       { fixed t = rp->p.x; rp->p.x = rp->q.x; rp->q.x = t; }
  231.     if ( rp->p.y > rp->q.y )
  232.       { fixed t = rp->p.y; rp->p.y = rp->q.y; rp->q.y = t; }
  233.     clp->sole.xmin = fixed2int_var_rounded(rp->p.x);
  234.     clp->sole.ymin = fixed2int_var_rounded(rp->p.y);
  235.     clp->sole.xmax = fixed2int_var_rounded(rp->q.x);
  236.     clp->sole.ymax = fixed2int_var_rounded(rp->q.y);
  237.     clp->count = 1;
  238. }
  239.  
  240. /* Add a clip list to a path. */
  241. /* The current implementation is very inefficient. */
  242. int
  243. gx_clip_list_add_to_path(gx_clip_list *clp, gx_path *ppath)
  244. {    gx_clip_rect *rp;
  245.     int code;
  246.     clip_prepare(clp);
  247.     for ( rp = &clp->first; rp != 0; rp = rp->next )
  248.        {    if ( rp->xmin < rp->xmax && rp->ymin < rp->ymax )
  249.            {    code = gx_path_add_rectangle(ppath,
  250.                     int2fixed(rp->xmin),
  251.                     int2fixed(rp->ymin),
  252.                     int2fixed(rp->xmax),
  253.                     int2fixed(rp->ymax));
  254.             if ( code < 0 ) return code;
  255.            }
  256.        }
  257.     return 0;
  258. }
  259.  
  260. /* Free a clip list. */
  261. void
  262. gx_clip_list_free(gx_clip_list *clp, gs_memory_procs *mp)
  263. {    gx_clip_rect *rp = clp->last.prev;
  264.     if ( clp->count <= 1 ) return;
  265.     clip_prepare(clp);
  266.     while ( rp != &clp->first )
  267.        {    gx_clip_rect *prev = rp->prev;
  268.         (*mp->free)((char *)rp, 1, sizeof(gx_clip_rect), "gx_clip_list_free");
  269.         rp = prev;
  270.        }
  271.     gx_clip_list_init(clp);
  272. }
  273.  
  274. /* Prepare a clip list for enumeration, */
  275. /* by splicing pointers to account for possible relocation. */
  276. private void
  277. clip_prepare(register gx_clip_list *clp)
  278. {    if ( clp->count <= 1 )
  279.        {    clp->first.next = clp->last.prev = &clp->sole;
  280.         clp->sole.prev = &clp->first;
  281.         clp->sole.next = &clp->last;
  282.        }
  283.     else
  284.        {    clp->first.next->prev = &clp->first;
  285.         clp->last.prev->next = &clp->last;
  286.        }
  287. }
  288.  
  289. /* ------ Rectangle list accumulator ------ */
  290.  
  291. /* Device for accumulating a clipping region. */
  292. private dev_proc_open_device(accum_open);
  293. private dev_proc_close_device(accum_close);
  294. private dev_proc_fill_rectangle(accum_fill_rectangle);
  295.  
  296. /* The device descriptor */
  297. /* Many of these procedures won't be called; they are set to NULL. */
  298. private gx_device_procs accum_procs = {
  299.     accum_open,
  300.     NULL,                /* get_initial_matrix */
  301.     NULL,                /* sync_output */
  302.     NULL,                /* output_page */
  303.     accum_close,
  304.     NULL,                /* map_rgb_color */
  305.     NULL,                /* map_color_rgb */
  306.     accum_fill_rectangle,
  307.     NULL,                /* tile_rectangle */
  308.     NULL,                /* copy_mono */
  309.     NULL,                /* copy_color */
  310.     NULL,                /* draw_line */
  311.     NULL,                /* get_bits */
  312.     NULL,                /* get_props */
  313.     NULL                /* put_props */
  314. };
  315. gx_device_accum gs_accum_device =
  316. {    sizeof(gx_device_accum),
  317.     &accum_procs,
  318.     "clip list accumulator",
  319.     0, 0, 1, 1, no_margins, dci_black_and_white, 0    /* generic */
  320. };
  321. #define adev ((gx_device_accum *)dev)
  322.  
  323. /* Initialize the accumulation device. */
  324. private int
  325. accum_open(register gx_device *dev)
  326. {    gx_clip_list_init(&adev->list);
  327.     adev->last = &adev->list.first;
  328.     adev->bbox.p.x = adev->bbox.p.y = max_int;
  329.     adev->bbox.q.x = adev->bbox.q.y = min_int;
  330.     return 0;
  331. }
  332.  
  333. /* Close the accumulation device. */
  334. private int
  335. accum_close(gx_device *dev)
  336. {    if ( adev->list.count >= 2 )
  337.        {    /* 'sole' isn't good for much of anything, */
  338.         /* and it complicates the bookkeeping.... */
  339.         gx_clip_rect *last = adev->last;
  340.         gx_clip_rect *ar =
  341.           (gx_clip_rect *)(*adev->memory_procs.alloc)(1, sizeof(gx_clip_rect), "accum_close");
  342.         if ( ar == 0 ) return_error(gs_error_VMerror);
  343.         *ar = adev->list.sole;
  344.         adev->list.sole.prev->next = ar;
  345.         if ( last == &adev->list.sole )
  346.             last = ar;
  347.         else
  348.             adev->list.sole.next->prev = ar;
  349.         adev->list.last.prev = last;
  350.        }
  351. #ifdef DEBUG
  352. if ( gs_debug['q'] )
  353.    {    gx_clip_rect *rp = &adev->list.first;
  354.     adev->last->next = 0;
  355.     while ( rp != 0 )
  356.        {    clip_rect_print("   ", rp);
  357.         rp = rp->next;
  358.        }
  359.    }
  360.     clip_prepare(&adev->list); /* just for clip_list_validate */
  361.     clip_list_validate(&adev->list);
  362. #endif
  363.     return 0;
  364. }
  365.  
  366. /* Accumulate one rectangle. */
  367. #define accum_alloc(s, ar, px, py, qx, qy)\
  368.    {    ar = (adev->list.count == 0 ? &adev->list.sole :\
  369.        (gx_clip_rect *)(*adev->memory_procs.alloc)(1, sizeof(gx_clip_rect), "accum_rect"));\
  370.     if ( ar == 0 ) return_error(gs_error_VMerror);\
  371.     ar->xmin = px, ar->ymin = py, ar->xmax = qx, ar->ymax = qy;\
  372.     adev->list.count++;\
  373.     clip_rect_print(s, ar);\
  374.    }
  375. #define accum_add_last(ar)\
  376.     adev->last->next = ar, ar->prev = adev->last, adev->last = ar
  377. #define accum_add_after(ar, rprev)\
  378.     ar->prev = rprev, ar->next = rprev->next;\
  379.     if ( rprev != adev->last ) rprev->next->prev = ar;\
  380.     else adev->last = ar;\
  381.     rprev->next = ar
  382. #define accum_add_before(ar, rnext)\
  383.     ar->prev = rnext->prev, ar->next = rnext,\
  384.       rnext->prev->next = ar, rnext->prev = ar
  385. /* Add a rectangle to the list.  It would be wonderful if rectangles */
  386. /* were always presented in the correct order, but they aren't, */
  387. /* because the fill loop works by trapezoids, not by scan lines. */
  388. /* All we can count on is that they are disjoint and *approximately* */
  389. /* in order. */
  390. #undef adev
  391. private int
  392. accum_add_rect(gx_device_accum *adev, int x, int y, int xe, int ye)
  393. {    gx_clip_rect *nr, *ar, *rptr;
  394.     int ymin, ymax;
  395. top:    rptr = adev->last;
  396.     accum_alloc("accum", nr, x, y, xe, ye);
  397.     if ( y >= rptr->ymax ||
  398.         y == rptr->ymin && ye == rptr->ymax && x >= rptr->xmax
  399.        )
  400.        {    accum_add_last(nr);
  401.         return 0;
  402.        }
  403.     /* Work backwards till we find the insertion point. */
  404.     while ( ye <= rptr->ymin ) rptr = rptr->prev;
  405.     ymin = rptr->ymin;
  406.     ymax = rptr->ymax;
  407.     if ( ye > ymax )
  408.        {    if ( y >= ymax )
  409.            {    /* Insert between two bands. */
  410.             accum_add_after(nr, rptr);
  411.             return 0;
  412.            }
  413.         /* Split off the top part of the new rectangle. */
  414.         accum_alloc("a.top", ar, x, ymax, xe, ye);
  415.         accum_add_after(ar, rptr);
  416.         ye = nr->ymax = ymax;
  417.         clip_rect_print(" ymax", nr);
  418.        }
  419.     /* Here we know ymin < ye <= ymax; */
  420.     /* rptr points to the last node with this value of ymin/ymax. */
  421.     /* Split the existing band if necessary. */
  422.     if ( ye < ymax )
  423.        {    gx_clip_rect *rsplit = rptr;
  424.         while ( rsplit->ymax == ymax )
  425.            {    accum_alloc("s.top", ar, rsplit->xmin, ye, rsplit->xmax, ymax);
  426.             accum_add_after(ar, rptr);
  427.             rsplit->ymax = ye;
  428.             rsplit = rsplit->prev;
  429.            }
  430.         ymax = ye;
  431.        }
  432.     if ( y > ymin )
  433.        {    gx_clip_rect *rbot = rptr, *rsplit;
  434.         while ( rbot->prev->ymin == ymin )
  435.             rbot = rbot->prev;
  436.         for ( rsplit = rbot; ; )
  437.            {    accum_alloc("s.bot", ar, rsplit->xmin, ymin, rsplit->xmax, y);
  438.             accum_add_before(ar, rbot);
  439.             rsplit->ymin = y;
  440.             if ( rsplit == rptr ) break;
  441.             rsplit = rsplit->next;
  442.            }
  443.         ymin = y;
  444.        }
  445.     /* Search for the X insertion point. */
  446.     /* The new rectangle is guaranteed disjoint from all the old ones. */
  447.     while ( rptr->ymin == ymin && x < rptr->xmax )
  448.        {    rptr = rptr->prev;
  449.        }
  450.     if ( y < ymin )
  451.        {    /* Continue with the bottom part of the new rectangle. */
  452.         nr->ymin = ymin;
  453.         clip_rect_print(" ymin", nr);
  454.         accum_add_after(nr, rptr);
  455.         ye = ymin;
  456.         goto top;
  457.        }
  458.     accum_add_after(nr, rptr);
  459.     return 0;
  460. }
  461. #define adev ((gx_device_accum *)dev)
  462. private int
  463. accum_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  464.   gx_color_index color)
  465. {    int xe, ye;
  466.     if ( w <= 0 || h <= 0 ) return 0;
  467.     xe = x + w, ye = y + h;
  468.     /* Update the bounding box. */
  469.     if ( x < adev->bbox.p.x ) adev->bbox.p.x = x;
  470.     if ( y < adev->bbox.p.y ) adev->bbox.p.y = y;
  471.     if ( xe > adev->bbox.q.x ) adev->bbox.q.x = xe;
  472.     if ( ye > adev->bbox.q.y ) adev->bbox.q.y = ye;
  473.     return accum_add_rect(adev, x, y, xe, ye);
  474. }
  475.  
  476. /* ------ Rectangle list clipper ------ */
  477.  
  478. /* Device for clipping with a region. */
  479. private dev_proc_open_device(clip_open);
  480. private dev_proc_map_rgb_color(clip_map_rgb_color);
  481. private dev_proc_map_color_rgb(clip_map_color_rgb);
  482. private dev_proc_fill_rectangle(clip_fill_rectangle);
  483. private dev_proc_tile_rectangle(clip_tile_rectangle);
  484. private dev_proc_copy_mono(clip_copy_mono);
  485. private dev_proc_copy_color(clip_copy_color);
  486. private dev_proc_get_bits(clip_get_bits);
  487. private dev_proc_get_props(clip_get_props);
  488. private dev_proc_put_props(clip_put_props);
  489.  
  490. /* The device descriptor. */
  491. private gx_device_procs clip_procs = {
  492.     clip_open,
  493.     gx_default_get_initial_matrix,
  494.     gx_default_sync_output,
  495.     gx_default_output_page,
  496.     gx_default_close_device,
  497.     clip_map_rgb_color,
  498.     clip_map_color_rgb,
  499.     clip_fill_rectangle,
  500.     clip_tile_rectangle,
  501.     clip_copy_mono,
  502.     clip_copy_color,
  503.     gx_default_draw_line,
  504.     clip_get_bits,
  505.     clip_get_props,
  506.     clip_put_props
  507. };
  508. gx_device_clip gs_clip_device =
  509. {    sizeof(gx_device_clip),
  510.     &clip_procs,
  511.     "clipper",
  512.     0, 0, 1, 1, no_margins, dci_black_and_white, 0    /* generic */
  513. };
  514. #define rdev ((gx_device_clip *)dev)
  515.  
  516. /* Declare and initialize the cursor variables. */
  517. #ifdef DEBUG
  518. private ulong clip_in, clip_down, clip_down2, clip_up, clip_x, clip_no_x;
  519. #  define inc(v) v++
  520. #else
  521. #  define inc(v) 0
  522. #endif
  523. #define DECLARE_CLIP\
  524.   register gx_clip_rect *rptr = rdev->current.rptr;\
  525.   gx_device *tdev = rdev->target;
  526. /* Check whether the rectangle x,y,w,h falls within the current entry. */
  527. #define xywh_in_ryptr()\
  528.   ((y >= rptr->ymin && y + h <= rptr->ymax &&\
  529.     x >= rptr->xmin && x + w <= rptr->xmax) ? (inc(clip_in), 1) : 0)
  530. /*
  531.  * Warp the cursor forward or backward to the first rectangle row that
  532.  * could include a given y value.  Assumes rptr is set, and updates it.
  533.  * Specifically, after warp_cursor, y < rptr->ymax and y >= rptr->prev->ymax.
  534.  * Note that ye <= rptr->ymin is possible.
  535.  */
  536. #define warp_cursor(y)\
  537.   while ( (y) >= rptr->ymax ) { inc(clip_up); rptr = rptr->next; };\
  538.   while ( rptr->prev != 0 && (y) < rptr->prev->ymax )\
  539.    { inc(clip_down); rptr = rptr->prev; }
  540. /*
  541.  * Enumerate the rectangles of the x,w,y,h argument that fall within
  542.  * the clipping region.  Usage:
  543.  *    BEGIN_CLIP
  544.  *        ... xc, yc, xec, yec ... [must be an expression statement]
  545.  *    NEXT_CLIP
  546.  *        (about to set yc to yec)
  547.  *    END_CLIP
  548.  */
  549. #ifdef DEBUG
  550. #  define clip_2_print(str, v1, v2)\
  551.     if ( gs_debug['q'] ) dprintf2(str, v1, v2)
  552. #else
  553. #  define clip_2_print(str, v1, v2) 0
  554. #endif
  555. #define BEGIN_CLIP\
  556.     if ( w <= 0 || h <= 0 ) return 0;\
  557.    {    int yc;\
  558.     const int xe = x + w, ye = y + h;\
  559.     warp_cursor(y);\
  560.     rdev->current.rptr = rptr;\
  561.     yc = rptr->ymin;\
  562.     if ( yc < y ) yc = y;\
  563.     else if ( yc >= ye ) return 0;\
  564.     for ( ; ; )\
  565.        {    const int ymax = rptr->ymax;\
  566.         int yec = ymax;\
  567.         if ( yec > ye ) yec = ye;\
  568.         clip_2_print("[q]yc=%d yec=%d\n", yc, yec);\
  569.         do \
  570.            {    int xc = rptr->xmin;\
  571.             int xec = rptr->xmax;\
  572.             if ( xc < x ) xc = x;\
  573.             if ( xec > xe ) xec = xe;\
  574.             if ( xec > xc )\
  575.                {    int code;\
  576.                 clip_rect_print("match", rptr);\
  577.                 clip_2_print("[q]xc=%d xec=%d\n", xc, xec);\
  578.                 inc(clip_x);\
  579.                 code =
  580. #define NEXT_CLIP\
  581.                 if ( code < 0 ) return code;\
  582.                }\
  583.             else inc(clip_no_x);\
  584.            }\
  585.         while ( (rptr = rptr->next) != 0 && rptr->ymax == ymax );\
  586.         if ( rptr == 0 || (yec = rptr->ymin) >= ye ) break;
  587. #define END_CLIP\
  588.         yc = yec;\
  589.        }\
  590.    }
  591.  
  592. /* Open a clipping device */
  593. private int
  594. clip_open(register gx_device *dev)
  595. {    gx_device *tdev = rdev->target;
  596.     /* Fix up possible dangling pointers. */
  597.     clip_prepare(&rdev->list);
  598.     /* Initialize the cursor. */
  599.     rdev->current.rptr = &rdev->list.first;
  600.     rdev->color_info = tdev->color_info;
  601.     return 0;
  602. }
  603.  
  604. /* Forward non-displaying operations to the target device. */
  605. private gx_color_index
  606. clip_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  607.   gx_color_value b)
  608. {    gx_device *tdev = rdev->target;
  609.     return (*tdev->procs->map_rgb_color)(tdev, r, g, b);
  610. }
  611. private int
  612. clip_map_color_rgb(gx_device *dev, gx_color_index color,
  613.   gx_color_value prgb[3])
  614. {    gx_device *tdev = rdev->target;
  615.     return (*tdev->procs->map_color_rgb)(tdev, color, prgb);
  616. }
  617. private int
  618. clip_get_props(gx_device *dev, gs_prop_item *plist)
  619. {    gx_device *tdev = rdev->target;
  620.     return (*tdev->procs->get_props)(tdev, plist);
  621. }
  622. private int
  623. clip_put_props(gx_device *dev, gs_prop_item *plist, int count)
  624. {    gx_device *tdev = rdev->target;
  625.     return (*tdev->procs->put_props)(tdev, plist, count);
  626. }
  627.  
  628. /* Fill a rectangle */
  629. private int
  630. clip_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  631.   gx_color_index color)
  632. {    DECLARE_CLIP
  633.     dev_proc_fill_rectangle((*fill)) = tdev->procs->fill_rectangle;
  634.     if ( xywh_in_ryptr() )
  635.         return (*fill)(tdev, x, y, w, h, color);
  636.     BEGIN_CLIP
  637.         (*fill)(tdev, xc, yc, xec - xc, yec - yc, color);
  638.     NEXT_CLIP
  639.     END_CLIP
  640.     return 0;
  641. }
  642.  
  643. /* Tile a rectangle */
  644. private int
  645. clip_tile_rectangle(gx_device *dev, gx_bitmap *tile,
  646.   int x, int y, int w, int h,
  647.   gx_color_index color0, gx_color_index color1, int phase_x, int phase_y)
  648. {    DECLARE_CLIP
  649.     dev_proc_tile_rectangle((*fill)) = tdev->procs->tile_rectangle;
  650.     if ( xywh_in_ryptr() )
  651.         return (*fill)(tdev, tile, x, y, w, h, color0, color1, phase_x, phase_y);
  652.     BEGIN_CLIP
  653.         (*fill)(tdev, tile, xc, yc, xec - xc, yec - yc, color0, color1, phase_x, phase_y);
  654.     NEXT_CLIP
  655.     END_CLIP
  656.     return 0;
  657. }
  658.  
  659. /* Copy a monochrome rectangle */
  660. private int
  661. clip_copy_mono(gx_device *dev,
  662.   byte *data, int sourcex, int raster, gx_bitmap_id id,
  663.   int x, int y, int w, int h,
  664.   gx_color_index color0, gx_color_index color1)
  665. {    DECLARE_CLIP
  666.     dev_proc_copy_mono((*copy)) = tdev->procs->copy_mono;
  667.     if ( xywh_in_ryptr() )
  668.         return (*copy)(tdev, data, sourcex, raster, id, x, y, w, h, color0, color1);
  669.     BEGIN_CLIP
  670.         (*copy)(tdev, data, sourcex + xc - x, raster, gx_no_bitmap_id, xc, yc, xec - xc, yec - yc, color0, color1);
  671.     NEXT_CLIP
  672.         data += (yec - yc) * raster;
  673.     END_CLIP
  674.     return 0;
  675. }
  676.  
  677. /* Copy a color rectangle */
  678. private int
  679. clip_copy_color(gx_device *dev,
  680.   byte *data, int sourcex, int raster, gx_bitmap_id id,
  681.   int x, int y, int w, int h)
  682. {    DECLARE_CLIP
  683.     dev_proc_copy_color((*copy)) = tdev->procs->copy_color;
  684.     if ( xywh_in_ryptr() )
  685.         return (*copy)(tdev, data, sourcex, raster, id, x, y, w, h);
  686.     BEGIN_CLIP
  687.         (*copy)(tdev, data, sourcex + xc - x, raster, gx_no_bitmap_id, xc, yc, xec - xc, yec - yc);
  688.     NEXT_CLIP
  689.         data += (yec - yc) * raster;
  690.     END_CLIP
  691.     return 0;
  692. }
  693.  
  694. /* Get bits back from the device. */
  695. private int
  696. clip_get_bits(gx_device *dev, int y, byte *data, uint size, int pad)
  697. {    gx_device *tdev = rdev->target;
  698.     return (*tdev->procs->get_bits)(tdev, y, data, size, pad);
  699. }
  700.